﻿using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace Module.Common.Common.QiPuHelper
{
    /// <summary>
    /// 棋谱完整树数据存储结构
    /// 每个结点可以有多个子结点，但没有兄弟节点，且只有一个父结点。
    /// 用于保存每一步走棋信息。
    /// </summary>
    public class TreeNodeOfQiPu : StepData
    {
        public TreeNodeOfQiPu ParentNode { get; set; } // 父结点
        public ObservableCollection<TreeNodeOfQiPu> _childrens;
        public ObservableCollection<TreeNodeOfQiPu> Childrens { get { return _childrens; } set { SetProperty(ref _childrens, value); } } // 子结点

        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value) CurNode = Visibility.Visible;
                else CurNode = Visibility.Collapsed;
                _isSelected = value;

            }
        }    // 是否选中
        private Visibility _curNode;
        public Visibility CurNode
        {
            get { return _curNode; }
            set { SetProperty(ref _curNode, value); }
        }

        public TreeNodeOfQiPu()
        {
            ParentNode = null;
            IsSelected = false;
            Cn = "棋谱记录";
            Childrens = new ObservableCollection<TreeNodeOfQiPu>();
        }



        public void CopyFromTreeNode(TreeNodeOfQiPu node)
        {
            SetStepData(node.QiZiId, node.X0, node.Y0, node.X1, node.Y1, node.DieQz);
            ID = node.ID;
        }

        /// <summary>
        /// 是否为根节点
        /// </summary>
        /// <returns>true=是根节点</returns>
        public bool IsRoot()
        {
            return ParentNode == null;
        }

        /// <summary>
        /// 获取父节点
        /// </summary>
        /// <returns>父节点</returns>
        public TreeNodeOfQiPu GetParent()
        {
            return ParentNode;
        }

        /// <summary>
        /// 获取6个历史着法，主要用于象棋引擎的重复走棋判断
        /// </summary>
        /// <returns></returns>
        public string Get6Moves()
        {
            string str = "";
            TreeNodeOfQiPu record = new();// this.Cursor;
            if (record.IsRoot()) return str;
            for (int i = 0; i < 20; i++)
            {
                if (!record.IsRoot())
                {
                    str += " " + record.UcciStep;
                    record = record.GetParent();
                }
            }
            return " moves " + str;
        }

        /// <summary>
        /// 连续杀4次，则判负。
        /// </summary>
        /// <returns></returns>
        public bool IsLianSha()
        {
            TreeNodeOfQiPu record = new();// this.Cursor;
            if (record.IsRoot()) return false;
            string[] stepString = new string[20];
            for (int i = 0; i < 20; i++)
            {
                stepString[i] = "";
                if (!record.IsRoot())
                {
                    stepString[i] = record.UcciStep;
                    record = record.GetParent();
                }
            }
            string step1 = stepString[0] + stepString[1] + stepString[2] + stepString[3];
            string step2 = stepString[4] + stepString[5] + stepString[6] + stepString[7];
            string step3 = stepString[8] + stepString[9] + stepString[10] + stepString[11];

            if (step1.Equals(step2) && step1.Equals(step3))
            {
                return true;
            }
            return false;
        }
    }
}
